<?php
namespace ac\modules\general\controllers;

use Yii;
use ac\controllers\AuthController;
use app\models\CouponTpl;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;


class CouponController extends AuthController
{

	/*列表*/
    public function actionCouponTplList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $couponTpl = new CouponTpl;
        $count = $couponTpl::find()
                            ->where('comid = :comid', [':comid' => $this->userInfo['comid']])
                            ->count();
        $couponTpl = $couponTpl::find()
                          ->where('comid = :comid', [':comid' => $this->userInfo['comid']])
                          ->offset($pageSize*($currPage-1))->limit($pageSize)
                          ->orderBy(['add_time' => SORT_DESC])
                          ->asArray()->all();
        $res = array(
            'list' => $couponTpl,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*添加*/
    public function actionAddCouponTpl()
    {
        // P($this->userInfo['comid']);
        // P($this->post);
        if(Yii::$app->request->isPost){
            $couponTpl = new CouponTpl;
            if($couponTpl->addTpl($this->post, $this->userInfo['comid'])){
                $backInfo = [0, '添加优惠券规则成功'];//返回信息
            }else{
                if($couponTpl->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $couponTpl->getErrors();//异常信息
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*
    修改
    */
    public function actionModifyCouponTpl()
    {

        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            $id = isset($this->get['id'])?$this->get['id']:'';
            if($id){
                $couponTpl = CouponTpl::find()
                           ->where('cid = :id', [':id' => $id])->asArray()->one();
                // P($couponTpl);
                if(!is_null($couponTpl)){
                    return Tool::showRes([0, '获取成功'], $couponTpl);
                }else{
                    return Tool::showRes(ErrMap::getError(10010));
                };
            }
        }

        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = $this->post['CouponTpl']['id'];
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $couponTpl = new CouponTpl;
                if($couponTpl->modifyTpl($this->post, $id)){
                    $backInfo = [0, '修改优惠券规则成功'];//返回信息
                }else{
                    if($couponTpl->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $couponTpl->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), $exception) ) != 'boolean'){
                return $res;
            };
        }

        return Tool::showRes(ErrMap::getError(10005));

    }
    
}
